(function() {

    var skill = {
        save: function() {
            db.update(config.dbname.admin, this._id, this);
        },
        destroy: function() {
            db.remove(config.dbname.admin, this._id);
        },
        setInfo: function function_name(info) {
            info.name = info.name ? info.name.trim() : "";
            info.email = info.email ? info.email.trim() : "";
            info.email_password = info.email_password ? info.email_password.trim() : "";
            //绑定名称
            if (!info.name) {
                throwCusError("管理员名称不可为空")
            }
            if (db.find_one(config.dbname.admin, {
                    info: {
                        name: info.name
                    }
                })) {
                throwCusError("管理员名字已经存在")
            }
            if (!helper.name(info.name, "管理员名称")) {
                throwCusError("管理员名称不符合规范")
            }
            this.info.name = info.name;
            //绑定的邮箱
            if (!helper.isEmail(info.email)) {
                throwCusError("邮箱格式不正确")
            }
            this.info.email = info.email;
            //绑定邮箱密码
            if (!info.email_password) {
                throwCusError("邮箱密码不可为空")
            }
            this.info.email_password = info.email_password;
            this.save();
        },
        //添加案例
        addCase: function(case_info) {
            var dotnar_case = new DotnarCaseCon(case_info);
            return dotnar_case._id;
        },
        //代码片段，获取案例
        _getCase: function(case_id) {
            var dotnar_case = DotnarCaseCon.get(case_id);
            if (!dotnar_case) {
                throwCusError("找不到指定案例")
            }
            return dotnar_case;
        },
        updateCase: function(case_id, case_info) {
            var dotnar_case = this._getCase(case_id);
            dotnar_case._setInfo(case_info);
        },
        removeCase: function(case_id) {
            var dotnar_case = this._getCase(case_id);
            dotnar_case.destroy();
        },
        //添加动态
        addNews: function(news_info) {
            var dotnar_news = new DotnarNewsCon(news_info);
            return dotnar_news._id;
        },
        _getNews: function(news_id) {
            var dotnar_news = DotnarNewsCon.get(news_id);
            if (!dotnar_news) {
                throwCusError("找不到指定动态")
            }
            return dotnar_news;
        },
        updateNews: function(news_id, news_info) {
            var dotnar_news = this._getNews(news_id);
            dotnar_news._setInfo(news_info);
        },
        removeNews: function(news_id) {
            var dotnar_news = this._getNews(news_id);
            dotnar_news.destroy();
        },
        //获取留言
        _getMessage: function(message_id) {
            var dotnar_message = DotnarMessageCon.get(message_id);
            if (!dotnar_message) {
                throwCusError("找不到指定留言")
            }
            return dotnar_message;
        },
        //回复留言
        replyMessage: function(message_id, reply_content) {
            var dotnar_message = this._getMessage(message_id);
            dotnar_message._addReply(this.info, reply_content);
        },
        //删除留言
        removeMessage: function(message_id) {
            var dotnar_message = this._getMessage(message_id);
            dotnar_message.destroy();
        },
        createLeague: function(league_name, leagueInfo) {
            //创建一个联盟对象
            var league = new LeagueCon(league_name, leagueInfo);

            return league._id;
        },
        //解散联盟
        dismissLeague: function(argument) {
            // body...
        },
        getBusList: function(num, page) {
            var bus_list = db.find_all(config.dbname.bus).slice();
            bus_list = data_cut.page(bus_list, num, page);
            var result = bus_list.map(function(busInfo, key) {
                var bus = BusCon.getInstance(busInfo);
                if (bus) {
                    bus.info.domain_url = bus.getOneVisitableUrl();
                    return bus;
                };
            });
            result.forEach(function(item, index) {
                if (!item) {
                    console.log("getBusList:", item)
                    result.splice(index, 1);
                } else {
                    if (item.constructor !== Object) {
                        result.splice(index, 1);
                    };
                };
            });
            return result;
        },
        //代码片段，获取商家
        _getBus: function(bus_id) {
            var bus = BusCon.get(bus_id);
            if (!bus) {
                throwCusError("找不到指定商家")
            }
            return bus;
        },
        removeBus: function(bus_id) {
            bus = this._getBus(bus_id);
            bus.destroy();
        },
        //修改商家的状态
        updateBusState: function(bus_id, state) {
            state = ~~state || 0;
            if (!bus_state_map[state]) {
                throwCusError("错误的商家状态值")
            }
            var bus = this._getBus(bus_id);
            bus.status = state;
            bus.save();
            return bus;
        },
        //修改商家权限与功能
        updateBusPermission: function(bus_id, permission_key, value) {
            var bus = this._getBus(bus_id);
            bus.setPerm(permission_key, value);
            return bus;
        },
        updateBusExpirationTime: function(bus_id, expiration_time) {
            var bus = this._getBus(bus_id);
            expiration_time = +helper.parseDateString(expiration_time);
            bus.expiration_time = expiration_time;
            bus.save();
            return bus;
        },
        /*
         * 修改商家风格包
         */
        //手机版
        setBusMobileTheme: function(bus_id, mobile_theme_name) {
            var bus = this._getBus(bus_id);
            bus.setMobileTheme(mobile_theme_name, true);
            return bus;
        },
        //增加商家域名
        getDomainList: function() {
            return db.find_all(config.dbname.domain).map(function(domain_info) {
                return DomainCon.getInstance(domain_info)
            });
        },
        addDomain: function(bus_id, domain, is_secondary_domain, port) {
            var bus = BusCon.get(bus_id);
            var domainIdList = bus.info.domainIdList || (bus.info.domainIdList = []);

            var domainInfo = new DomainCon({
                domain: domain,
                is_secondary_domain: is_secondary_domain,
                bus_id: bus_id,
                port: ~~port
            });
            domainIdList.push(domainInfo._id);
            bus.save();
            return domainInfo;
        },
        removeDomain: function(domain_id) {
            var domain = DomainCon.get(domain_id);
            var bus = BusCon.get(domain.bus_id);
            var domainIdList = bus.info.domainIdList || [];
            var _index = domainIdList.indexOf(domain_id);
            if (_index != -1) {
                //商家删除DOMAIN
                domainIdList.splice(_index, 1);
                bus.save();
            }
            //DOMAIN从数据库中销毁
            domain.destroy();
            return bus;
        },
        clearBusGoods: function(bus_id) {
            var bus = BusCon.get(bus_id);
            if (bus) {
                bus.getGoodsList().forEach(function(goods) {
                    goods.destroy();
                });
                bus.store = [];
                //删除分销商品
                BinaCon.findByBusId(bus._id).forEach(function(bina) {
                    bina.destroy();
                });
                bus.save();
            }
            return bus;
        },
        clearBusAch: function(bus_id) {
            var bus = BusCon.get(bus_id);
            if (bus) {
                bus.ach = [];
                bus.save();
            }
            return bus;
        },
        clearBusData: function(bus_id) {
            var bus = BusCon.get(bus_id);
            if (bus) {
                //删除商品
                bus.getGoodsList().forEach(function(goods) {
                    goods.destroy();
                });
                bus.store = [];
                //删除订单
                bus.ach = [];
                //删除评论
                bus.comments.forEach(function(comment_id) {
                    var comment = CommentCon.get(comment_id);
                    comment && comment.destroy();
                });
                bus.comments = [];
                //删除标签
                bus.tags.forEach(function(tag_id) {
                    var tag = BusTagCon.get(tag_id);
                    tag && tag.destroy();
                });
                bus.tags = [];
                //删除BUFF
                bus.buffs.forEach(function(buff_id) {
                    var buff = BuffCon.get(buff_id);
                    buff && buff.destroy();
                });
                bus.buffs = [];
                //
                bus.recommend_buffs = [];
                bus.recommend_config = [];
                bus.recommend_discoun = 0;
                bus.recommend_config_buff_id = null;
                bus.guide_config = [];
                bus.guide_config_buff_id = null;
                bus.guide_list = [];
                bus.config = {};
                //删除推荐返利
                BusUserBindCon.findByBusId(bus._id).forEach(function(bind_info) {
                    var bind = BusUserBindCon.getInstance(bind_info);
                    bind.destroy();
                });
                //删除分销商品
                BinaCon.findByBusId(bus._id).forEach(function(bina) {
                    bina.destroy();
                });
                bus.save();
                return bus;
            }
        },
        setBusNavPerms: function(bus_id, perms) {
            var bus = BusCon.get(bus_id);
            bus.perms = perms;
            bus.save();
            return bus;
        }

    }
    module.exports = skill;
    var db = require("../db/index");
    var UserCon = require("./_con");
    var BusCon = require("../bus/_con");
    var BinaCon = require("../bina/_con");
    var BuffCon = require("../buff/_con");
    var CommentCon = require("../comment/_con");
    var BusTagCon = require("../bus.tag/_con");
    var DomainCon = require("../domain/_con");
    var GoodsCon = require("../goods/_con");
    var DotnarCaseCon = require("../dotnar_case/_con");
    var DotnarNewsCon = require("../dotnar_news/_con");
    var DotnarMessageCon = require("../dotnar_message/_con");
    var AchCon = require("../ach/_con");
    var LeagueCon = require("../league/_con");
    var config = require("../_include/config");
    var helper = require("../_include/helper");
    var data_cut = require("../_include/data_cut");
    var BusUserBindCon = require("../bus_user_bind/_con");
    var bus_state_map = {};
    for (var key in BusCon.STATE) {
        var value = BusCon.STATE[key];
        bus_state_map[value] = key;
    }
}());
